package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.Drtypeinfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DrtypemodeMapper;
import com.ruoyi.system.domain.Drtypemode;
import com.ruoyi.system.service.IDrtypemodeService;

/**
 * 设备类型模板Service业务层处理
 * 
 * @author yaozh
 * @date 2024-02-29
 */
@Service
public class DrtypemodeServiceImpl implements IDrtypemodeService 
{
    private static final Logger log = LoggerFactory.getLogger(DrtypemodeServiceImpl.class);
    @Autowired
    private DrtypemodeMapper drtypemodeMapper;

    /**
     * 查询设备类型模板
     * 
     * @param typemodeid 设备类型模板主键
     * @return 设备类型模板
     */
    @Override
    public Drtypemode selectDrtypemodeByTypemodeid(Long typemodeid,String dbName)
    {
        return drtypemodeMapper.selectDrtypemodeByTypemodeid(typemodeid,dbName);
    }

    /**
     * 查询设备类型模板列表
     * 
     * @param drtypemode 设备类型模板
     * @return 设备类型模板
     */
    @Override
    public List<Drtypemode> selectDrtypemodeList(Drtypemode drtypemode,String dbName)
    {
        return drtypemodeMapper.selectDrtypemodeList(drtypemode,dbName);
    }

    /**
     * 新增设备类型模板
     * 
     * @param drtypemode 设备类型模板
     * @return 结果
     */
    @Override
    public int insertDrtypemode(Drtypemode drtypemode,String dbName)
    {
        return drtypemodeMapper.insertDrtypemode(drtypemode,dbName);
    }

    /**
     * 修改设备类型模板
     * 
     * @param drtypemode 设备类型模板
     * @return 结果
     */
    @Override
    public int updateDrtypemode(Drtypemode drtypemode,String dbName)
    {
        return drtypemodeMapper.updateDrtypemode(drtypemode,dbName);
    }

    /**
     * 批量删除设备类型模板
     * 
     * @param typemodeids 需要删除的设备类型模板主键
     * @return 结果
     */
    @Override
    public int deleteDrtypemodeByTypemodeids(Long[] typemodeids,String dbName)
    {
        return drtypemodeMapper.deleteDrtypemodeByTypemodeids(typemodeids,dbName);
    }

    /**
     * 删除设备类型模板信息
     * 
     * @param typemodeid 设备类型模板主键
     * @return 结果
     */
    @Override
    public int deleteDrtypemodeByTypemodeid(Long typemodeid,String dbName)
    {
        return drtypemodeMapper.deleteDrtypemodeByTypemodeid(typemodeid,dbName);
    }

    @Override
    public String importDrTypeMode(List<Drtypemode> drTypeModeList, Boolean isUpdateSupport, String operName, String dbName){
        if (drTypeModeList == null || drTypeModeList.size() == 0)
        {
            throw new ServiceException("导入项目设备属性定义数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Drtypemode drTypeMode : drTypeModeList)
        {
            try{
                // 验证是否存在，drTypeId + regName 联合唯一主键（业务逻辑上）
                Long drTypeId = drTypeMode.getDrtypeid();
                String regName = drTypeMode.getRegName();
                Drtypemode tempDrTypeMode = drtypemodeMapper.selectDrTypeModeByDrTypeIdAndRegName(drTypeId,regName,dbName);
                if (tempDrTypeMode == null)
                {
                    drTypeMode.setCreateBy(operName);
                    drtypemodeMapper.insertDrtypemode(drTypeMode,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备类型ID " + drTypeMode.getDrtypeid() + "、设备类型 " + drTypeMode.getDrtypename() + "、设备属性名称 " + drTypeMode.getRegName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    drTypeMode.setDrtypeid(tempDrTypeMode.getDrtypeid());
                    drTypeMode.setUpdateBy(operName);
                    drtypemodeMapper.updateDrtypemode(drTypeMode,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备类型ID " + drTypeMode.getDrtypeid() + "、设备类型 " + drTypeMode.getDrtypename() + "、设备属性名称 " + drTypeMode.getRegName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、设备类型ID " + drTypeMode.getDrtypeid() + "、设备类型 " + drTypeMode.getDrtypename() + "、设备属性名称 " + drTypeMode.getRegName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备类型ID " + drTypeMode.getDrtypeid() + "、设备类型 " + drTypeMode.getDrtypename() + "、设备属性名称 " + drTypeMode.getRegName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
